﻿using SpaceNeedle.City6473.Web.Data.Models;
using SpaceNeedle.SDK.Web.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SpaceNeedle.City6473.Web.Data.Repositories
{
    public class RequestItemsSupportersRepository : EntitiyFrameworkRepository<RequestItemSupporter>
    {
        public RequestItemsSupportersRepository()
            : base(new City6473Entities())
        {
            this.dataContext.Configuration.ProxyCreationEnabled = false;
        }

        public IQueryable<RequestItem> GetRequestItemsByUser(Guid userId)
        {
            return this.GetAll().Where(x => x.CreatedBy == userId).Select(x => x.RequestItem);
        }

        public IQueryable<Guid> GetRequestItemSupporters(Guid requestItemId)
        {
            return this.GetAll().Where(x => x.RequestItemId == requestItemId).Select(x => x.CreatedBy);
        }

        public int GetRequestItemSupportersCount(Guid requestItemId)
        {
            return this.GetRequestItemSupporters(requestItemId).Count();
        }

        public override RequestItemSupporter GetById(Guid id)
        {
            return this.GetAll().Where(x => x.Id == id).SingleOrDefault();
        }

        public override RequestItemSupporter GetById(string id)
        {
            if(string.IsNullOrEmpty(id))
            {
                return null;
            }

            return this.GetById(new Guid(id));
        }

        public override void Delete(RequestItemSupporter entity)
        {
            entity = this.GetAll().Where(x => x.RequestItemId == entity.RequestItemId && x.CreatedBy == entity.CreatedBy).SingleOrDefault();

            if (entity != null)
            {
                base.Delete(entity);
            }
        }
    }
}
